package com.damie.controller;

import com.damie.dto.WasteOrderDto;
import com.damie.param.WasteOrderParam;
import com.damie.service.UserService;
import com.damie.utils.Result;
import com.damie.utils.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  用户主页功能
 * </p>
 *
 * @author Alon
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/damie/user")
@Slf4j
@CrossOrigin("*")
@Api(description = "用户下单服务模块")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 废品回收
     * @param wasteOrderDto 回收废品参数对象
     * @return
     */
    @PostMapping("/wasteOrder")
    @ApiOperation(value = "废品回收接口")
    public Result wasteOrder(@RequestBody WasteOrderDto wasteOrderDto, HttpServletRequest request){
        log.info("{}",wasteOrderDto);
        if (ObjectUtils.isEmpty(wasteOrderDto)){
            return new Result(false, StatusCode.NULL_ERROR,"数据不能为空",null);
        }
        WasteOrderParam wasteOrderParam = new WasteOrderParam();
        BeanUtils.copyProperties(wasteOrderDto,wasteOrderParam);
        Boolean b = userService.wasteOrder(wasteOrderParam,request);
        return Result.success("下单成功");
    }

    /**
     * 下单图片上传
     * @return
     */
    @ApiOperation(value = "下单图片上传")
    @PostMapping("/uploadPicture")
    public Result uploadPicture(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        log.info("{}",file);
        Integer[] number = {1,2,3};
        if (file != null){
            //获取到照片路径
            String pictureUrl = userService.uploadPicture(file);
            if (StringUtils.isEmpty(pictureUrl)){
                return Result.success(pictureUrl);
            }
            return Result.fail("图片上传失败");
        }else {
            return new Result(false,StatusCode.NULL_ERROR,"传入数据不能为空",null);
        }
    }

}

